﻿using System;
using System.Data.SqlClient;

namespace ErrorDumper.PropertyExtracting {
    public class SqlExceptionPropertyExtractor : IExceptionPropertyExtractor {

        public void ExtractProperties(object instance, ExceptionPropertyCollection properties) {
            if (instance == null)
                throw new ArgumentNullException("instance");
            if (properties == null)
                throw new ArgumentNullException("properties");

            var exception = instance as SqlException;
            if (exception == null)
                return;

            if (!properties.Contains("SqlErrorCode"))
                properties["SqlErrorCode"] = exception.ErrorCode.ToString();
            if (!properties.Contains("SqlErrorClass"))
                properties["SqlErrorClass"] = exception.Class.ToString();
            if (!properties.Contains("SqlErrorLineNumber"))
                properties["SqlErroLineNumber"] = exception.LineNumber.ToString();
            if (!properties.Contains("SqlErrorNumber"))
                properties["SqlErrorNumber"] = exception.Number.ToString();
            if (!string.IsNullOrEmpty(exception.Procedure) && !properties.Contains("SqlErrorProcedure"))
                properties["SqlErrorProcedure"] = exception.Procedure;
            if (!properties.Contains("SqlErrorServer"))
                properties["SqlErrorServer"] = exception.Server;
            if (!properties.Contains("SqlErrorState"))
                properties["SqlErrorState"] = exception.State.ToString();
        }
    }
}